package com.ccy.legendary.game;

import com.ccy.legendary.Operation.pojo.ActRemark;
import com.ccy.legendary.base.BaseController;
import com.ccy.legendary.socker.WSMessageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/game")
public class GameController extends BaseController {

    private final WSMessageService wsMessageService;
    private final GameService gameService;

    public GameController(WSMessageService wsMessageService,GameService gameService) {
        this.wsMessageService = wsMessageService;
        this.gameService = gameService;
    }

    @RequestMapping("/welcome")
    public ModelAndView HelloWorld() {
        return new ModelAndView("/game/welcome");
    }

    @RequestMapping("/main")
    public ModelAndView main(HttpServletRequest request, @RequestParam("userCode")String userCode) throws Exception{
        ModelAndView mv = new ModelAndView("/game/main");
        String message = ActRemark.addARemark(userCode);
        gameService.initGame(userCode);
        wsMessageService.sendAll(message);
        request.getSession().setAttribute("userCode",userCode);
        mv.addObject("userCode",userCode);
        return new ModelAndView("/game/main");
    }
}
